package com.miracle.system.web;

import com.miracle.common.utils.SearchUtils;
import com.miracle.common.utils.SecurityUtils;
import com.miracle.common.utils.StringUtils;
import com.miracle.core.security.service.TokenService;
import com.miracle.system.base.domain.ResultMap;
import com.miracle.system.domain.SysDictH;
import com.miracle.system.domain.SysDictL;
import com.miracle.system.domain.SysUser;
import com.miracle.system.service.SysDictHService;
import com.miracle.system.service.SysDictLService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：miracle.cui
 * @description：TODO
 * @date ：2020/8/10 8:46
 */
@ApiModel(value = "字典管理类")
@RestController
@RequestMapping("/system/dict")
public class SysDictController {

    @Autowired
    SysDictHService dictHService;
    @Autowired
    SysDictLService dictLService;

    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "字典列表")
    @GetMapping("/list")
    public ResultMap list(SysDictH dictH){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        SearchUtils.startPage();
        List<SysDictH> dictHList = dictHService.selectDictHList(dictH);
        return ResultMap.getPage(200,"成功", dictHList);
    }

    @ApiOperation(value = "新增字典")
    @PostMapping
    public ResultMap add(@Validated @RequestBody SysDictH dictH){

        dictH.setCreateBy(SecurityUtils.getUserId());
        Integer rows = dictHService.insertDictH(dictH);
        return rows > 0? ResultMap.success(dictH) : ResultMap.error();
    }

    @ApiOperation(value = "删除字典")
    @DeleteMapping
    public ResultMap delete(@Validated @RequestBody Integer[] dictTypeIds){

        Integer rows = dictHService.deleteDictHByDictTypeIds(dictTypeIds);
        return rows > 0 ? ResultMap.success() : ResultMap.error();
    }

    @ApiOperation(value = "修改字典")
    @PutMapping
    public ResultMap edit(@Validated @RequestBody SysDictH dictH){

        dictH.setLastUpdateBy(SecurityUtils.getUserId());
        Integer rows = dictHService.updateDictH(dictH);

        return rows > 0 ? ResultMap.success() : ResultMap.error();
    }

    @ApiOperation(value = "查询字典")
    @GetMapping(value = {"/", "/{dictTypeId}"})
    public ResultMap getInfo(@PathVariable(value = "dictTypeId", required = false) Integer dictTypeId){

        if (StringUtils.isNotNull(dictTypeId)){
            return ResultMap.success(dictHService.selectDictHByDictTypeId(dictTypeId));
        } else {
            return ResultMap.error();
        }
    }

    @ApiOperation(value = "字典从表列表")
    @GetMapping("/llist")
    public ResultMap llist(SysDictH dictH){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        SearchUtils.startPage();
        List<SysDictL> dictLList = dictLService.selectDictLByDictH(dictH);
        return ResultMap.getPage(200,"成功", dictLList);
    }

    @ApiOperation(value = "根据字典类型查询字典详细值")
    @GetMapping("/getDictDetail")
    public ResultMap dictTypeCode(String dictTypeCode){
        List<SysDictL> dictLList = dictLService.selectDictLByDictTypeCode(dictTypeCode);
        return ResultMap.success("成功", dictLList);
    }

    @ApiOperation(value = "刷新缓存")
    @GetMapping("/refreshCache")
    public ResultMap refreshCache(){
        dictHService.refreshCache();
        return ResultMap.success();
    }

    /* 远程校验 */
    @ApiOperation(value = "远程校验字典主表")
    @GetMapping(value = {"/remoteCheckDictH"})
    public ResultMap remoteCheckDictH(SysDictH dictH){
        dictH.setStatus("enabled");
        SysDictH checkDictH = dictHService.remoteCheckDictH(dictH);
        return ResultMap.success(checkDictH);
    }
}
